package serverdata.ch.home.www.server;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


import java.util.List;









public class ClientHosting implements Runnable {

	private Socket client;
	private List<ClientHosting> listaclient;
	private PrintWriter out;
	private BufferedReader in;

	private HttpData inputHttp;
	private PreparedInterface preparedInterface;


	public ClientHosting(Socket client, List<ClientHosting> listaclient) {


		this.client = client;
		this.listaclient = listaclient;
		

		try {
			out = new PrintWriter(client.getOutputStream());
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	public void run() {

		try{

			inputHttp = new HttpData(in);

			if(inputHttp.getComando().equalsIgnoreCase("GET")){
				
				
				preparedInterface.doGet( in, out, inputHttp);
				
				client.close();
				listaclient.remove(this);
				

			}
			else if(inputHttp.getComando().equalsIgnoreCase("POST")){
				
				preparedInterface.doPost( in, out, inputHttp);
				client.close();
				listaclient.remove(this);
			}
		}catch(IOException e){
			System.err.println("errore nel ClientHosting");
		}
	



	}
	
	public void addPreparedInterface(PreparedInterface p){
		preparedInterface = p;
	}

	




}
